from django.contrib.auth.models import  User
from django.db import models
from django.db.models.base import Model
from django.db.models.deletion import CASCADE
from django.db.models.fields import IntegerField

 # Create your models here.
class Role(models.Model):
    """
        用户角色的模型类分为:
        1、商家(洗衣服务的提供者)
        2、客户(洗衣服务的消费者)
    """
    roles = [
        (0, '商家'),
        (1, '客户')
    ]
    username = models.CharField(max_length=20, primary_key=True)
    password = models.CharField(max_length=20, null=False)
    role = models.SmallIntegerField(verbose_name="身份", choices=roles)
    class Meta:
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.username
# 抽象类
class MyAbstractuser(models.Model):
    address = models.CharField(max_length=20, verbose_name="地址", null="false")
    tel = models.CharField(max_length=11, verbose_name="手机号", null="false")
    realname = models.CharField(max_length=10, verbose_name="真实姓名", null="false")
    class Meta:
        abstract = True

class Merchant(MyAbstractuser):
    """
        商家类
    """
    user = models.OneToOneField(Role, related_name='merchant', on_delete=models.CASCADE)
    rate_speed = models.FloatField(verbose_name="速度评分",default=0)
    rate_price = models.FloatField(verbose_name="价格评分",default=0)
    rate_quality = models.FloatField(verbose_name="质量评分",default=0)
    mult_rate = models.FloatField(verbose_name="综合评分",default=0)
    class Meta:
        verbose_name = "商家表"
        verbose_name_plural = verbose_name
        ordering = ["-id"]
        db_table = "Merchant"
    
    def __str__(self):
        return self.user.username

class Customer(MyAbstractuser):
    """
        客户类
    """
    SEX = [
        (0, '女'),
        (1, '男')
    ]
    user = models.OneToOneField(Role, related_name='customer', on_delete=models.CASCADE)
    sex  = models.IntegerField(verbose_name='性别', choices=SEX, default=0)
    # customer_credit = models.FloatField(verbose_name='信誉',  default=100)  # 信誉低于80无法下单

    class Meta:
        verbose_name = "客户表"
        verbose_name_plural = verbose_name
        ordering = ["-id"]
        db_table = "customer"
    
    def __str__(self):
        return self.user.username

class MCloth(models.Model):
    """
        商家提供的洗衣服务的衣服类
    """
    WM = [
        (0, '普通洗'),
        (1, '超强洗')
    ]
    type = models.CharField(max_length=20, verbose_name="类型")
    Washmethod = models.IntegerField(verbose_name="洗涤模式")
    price = models.FloatField(verbose_name="单价")
    merchant = models.ForeignKey(Merchant, verbose_name="所属商家", on_delete=models.CASCADE)
    class Meta:
        verbose_name = "衣服价格表"
        verbose_name_plural = verbose_name
        ordering = ["-merchant_id"]
        db_table = "clothprice"

class Cloth(models.Model):
    """
        订单中的衣服信息类
    """
    type = models.CharField(max_length=20, verbose_name="类型")
    Washmethod = models.IntegerField(verbose_name="洗涤模式")
    price = models.FloatField(verbose_name="单价")
    num = models.IntegerField(verbose_name="衣服数量")
    ClothPrice = models.IntegerField(verbose_name="参考价格表的id")
    order_id = models.IntegerField(verbose_name="所属订单")
    class Meta:
        verbose_name = "衣服表"
        verbose_name_plural = verbose_name
        ordering = ["-id"]
        db_table = "cloth"
    
class Order(models.Model):
    """
        订单类
    """
    merchant = models.ForeignKey(Merchant, verbose_name="商家", on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, verbose_name="顾客", on_delete=models.CASCADE)
    status = models.CharField(max_length=50, verbose_name="状态")
    total = models.FloatField(verbose_name="总价")
    create_time = models.DateTimeField(verbose_name="创建时间")
    update_time = models.DateTimeField(verbose_name="最后修改时间")
    is_Rated = models.IntegerField(verbose_name="是否评过分",default=0) #0是没有评过分
    rate_speed = models.IntegerField(verbose_name="速度评分",null=True)
    rate_price = models.IntegerField(verbose_name="价格评分",null=True)
    rate_quality = models.IntegerField(verbose_name="质量评分",null=True)

    # remarks = models.CharField(max_length=100, verbose_name="备注", null=True)

    class Meta:
        verbose_name = "订单表"
        verbose_name_plural = verbose_name
        ordering = ["-create_time"]
        db_table = "order"
